package com.skyline.user.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.skyline.base.controller.BaseController;
import com.skyline.base.exception.NotLoginException;
import com.skyline.base.type.Authority;
import com.skyline.common.bean.Page;
import com.skyline.common.util.AuthorityUtil;
import com.skyline.common.util.ViewPaths;
import com.skyline.user.model.Fan;
import com.skyline.user.model.User;
import com.skyline.user.service.FanService;
import com.skyline.user.type.IdolType;

@Controller
public class FanController extends BaseController {
	private static final Log LOGGER = LogFactory.getLog(BasicUserController.class);

	@Autowired
	private FanService fanService;

	private @Value("${fan.listfan.pagesize}")
	int listFanPageSize;

	private @Value("${fan.listidol.pagesize}")
	int listIdolPageSize;

	/**
	 * 添加关注
	 * 
	 * @param idolId
	 * @param idolNickname
	 * @param idolPortrait
	 * @param idolType
	 * @return
	 */
	@RequestMapping(value = "/follow", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> follow(Long idolId, String idolNickname, String idolPortrait, IdolType idolType) {
		try {
			User user = checkAndReturnLoginedUser();
			if (fanService.isFan(idolId, user.getId())) {
				return toJsonMap(false, "您已经关注了此用户", true);
			}
			fanService.follow(idolId, idolNickname, idolPortrait, user.getId(), user.getNickname(), user.getPortrait(),
					idolType);
			
			//更新权限列表
			AuthorityUtil.updateAuthority(idolId, Authority.ONLY_TO_FANS, null);
			return toJsonMap(true, "关注用户成功", true);
		} catch (NotLoginException e) {
			LOGGER.info("关注用户失败，需要登录", e);
			return toJsonMap(false, "关注用户请先登录", false);
		} catch (Exception e) {
			LOGGER.error("添加关注失败", e);
			return toJsonMap(false, "关注失败", true);
		}

	}

	/**
	 * 取消关注
	 * 
	 * @param idolId
	 * @return
	 */
	@RequestMapping(value = "/unfollow/{idolId}", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> unFollow(@PathVariable("idolId") Long idolId, Long ownerId) {
		try {
			User user = checkAndReturnLoginedUser();
			fanService.unFollow(idolId, user.getId());
			
			//更新权限列表
			AuthorityUtil.updateAuthority(idolId, Authority.PUBLIC, null);
			
			Map<String, Object> result = toJsonMap(true, "取消关注成功", true);
			return result;
		} catch (NotLoginException e) {
			LOGGER.info("取消关注失败，需要登录");
			return toJsonMap(false, "取消登录需要先登录", false);
		} catch (Exception e) {
			LOGGER.warn("取消关注失败:", e);
			return toJsonMap(false, "取消登录失败", true);
		}
	}


	@RequestMapping(value = "/fans/{ownerId}", method = RequestMethod.GET)
	public ModelAndView listFans(@PathVariable("ownerId") Long ownerId, Page page) {
		page.setSize(listFanPageSize);

		ModelAndView view;
		Long viewerId = getViewerId();
		if (viewerId != 0L && viewerId.equals(ownerId)) {
			view = new ModelAndView(ViewPaths.FAN_MYFANSLIST);
		} else {
			view = new ModelAndView(ViewPaths.FAN_FANSLIST);
			view.addObject("logined", (viewerId != 0L));
		}
		List<Fan> fanList = fanService.listFansWithFollowed(ownerId, viewerId, page);
		view.addObject("fanList", fanList);
		view.addObject("page", page);
		view.addObject("ownerId", ownerId);
		view.addObject("viewerId", viewerId);
		return view;
	}

	@RequestMapping(value = "/follow/{ownerId}", method = RequestMethod.GET)
	public ModelAndView listIdols(@PathVariable("ownerId") Long ownerId,
			@RequestParam(value = "type", required = false) IdolType type, Page page) {
		Long viewerId = getViewerId();
		ModelAndView view;
		List<Fan> idolList;

		page.setSize(listIdolPageSize);

		if (viewerId != 0L && viewerId.equals(ownerId)) {
			view = new ModelAndView(ViewPaths.FAN_MYIDOLSSLIST);
			idolList = fanService.listIdols(ownerId, type, page);
		} else {
			view = new ModelAndView(ViewPaths.FAN_IDOLSSLIST);
			view.addObject("logined", (viewerId != 0L));
			idolList = fanService.listIdolsWithFollowed(ownerId, viewerId, type, page);
		}

		if (type != null) {
			view.addObject("type", type.toString());
		}
		view.addObject("idolList", idolList);
		view.addObject("page", page);
		view.addObject("ownerId", ownerId);
		view.addObject("viewerId", viewerId);
		return view;
	}

}
